/*
 * @(#)RePlugin.java 2020年3月2日 下午8:59:03
 * Copyright 2020 zmr, Inc. All rights reserved. 
 * PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.wind.cqboot.plugin;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.stereotype.Component;

import net.lz1998.cq.event.message.CQGroupMessageEvent;
import net.lz1998.cq.robot.CQPlugin;
import net.lz1998.cq.robot.CoolQ;

/**
 * <p>File：RePlugin.java</p>
 * <p>Title: </p>
 * <p>Description:</p>
 * <p>Copyright: Copyright (c) 2020 2020年3月2日 下午8:59:03</p>
 * <p>Company: zmrit.com </p>
 * @author zmr
 * @version 1.0
 */
@Component
public class RepeatPlugin extends CQPlugin {
    // Ctrl+O

    Map<Long,String> lastMsgMap=new ConcurrentHashMap<>();
    Map<Long,Integer> countMap=new ConcurrentHashMap<>();

    @Override
    public int onGroupMessage(CoolQ cq, CQGroupMessageEvent event) {
        String msg = event.getMessage();
        Long groupId = event.getGroupId();

        String lastMsg=lastMsgMap.getOrDefault(groupId,"");
        Integer count=countMap.getOrDefault(groupId,0);

        if (msg.equals(lastMsg)) {
            count++;
            countMap.put(groupId,count);
            if (count > 2) {
                cq.sendGroupMsg(groupId, "刷屏会得到一张机票", false);
                countMap.put(groupId,0);
            }
        } else {
            countMap.put(groupId,1);
            lastMsgMap.put(groupId,msg);
        }
        return super.onGroupMessage(cq, event);
    }
}
